package com.stu.wgt.test.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stu.wgt.test.bean.Address;
import com.stu.wgt.test.result.R;
import com.stu.wgt.test.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgt
 * @since 2024-01-25
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService addressService;

    @PostMapping("/add")
    public R addAddress(@RequestBody Address address) {
        int result = addressService.insert(address);
        if (result == 1) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{id}")
    public R deleteAddress(@PathVariable("id") int id) {
        int result = addressService.delete(id);
        if (result == 1) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping("/update/{id}")
    public R updateAddress(@RequestBody Address address, @PathVariable("id") Integer id) {
        address.setId(id);
        int result = addressService.update(address);
        if (result == 1) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("/all")
    public R getAllAddresses() {
        List<Address> addressList = addressService.selectAllAddress();
        if (!addressList.isEmpty()) {
            return R.ok().data("address", addressList);
        }
        return R.error();
    }

    @GetMapping("/{id}")
    public R getAddressById(@PathVariable("id") int id) {
        try {
            List<Address> addressList = addressService.selectById(id);
            return R.ok().data("address", addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/selectByUserId/{id}")
    public R getAddressByUserId(@PathVariable("id") int id) {
        try {
            List<Address> addressList = addressService.selectByUserId(id);
            return R.ok().data("address", addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/search")
    public R searchAddressByName(@RequestParam("name") String name) {
        try {
            List<Address> addressList = addressService.selectByName(name);
            return R.ok().data("address", addressList);
        } catch (Exception e) {
            return R.error();
        }
    }

    @GetMapping("/search/{query}")
    public R searchAddress(@PathVariable("query") String query,
                           @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        try {
            IPage<Address> addressList = addressService.searchAddress(query, pageNum, pageSize);
            return R.ok().data("address", addressList.getRecords()).data("total", addressList.getTotal());
        } catch (Exception e) {
            return R.error();
        }
    }
}

